package cn.edu.tju.elm.repository;

import cn.edu.tju.elm.model.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

/**
 * ClassName: CommentRepository
 * Package: cn.edu.tju.elm.repository
 * Description: 评论数据访问层
 *
 * @Author System Generated
 * @Create 2025/10/30
 * @Version 1.0
 */
public interface CommentRepository extends JpaRepository<Comment, Long> {
    
    // 根据订单ID查询评论
    Optional<Comment> findByOrderIdAndDeletedFalse(Long orderId);
    
    // 根据用户ID查询所有评论
    List<Comment> findByCustomerIdAndDeletedFalse(Long customerId);
    
    // 根据店铺ID查询所有评论
    List<Comment> findByBusinessIdAndDeletedFalse(Long businessId);
    
    // 根据评分查询评论
    List<Comment> findByRatingAndDeletedFalse(Integer rating);
    
    // 根据店铺ID和评分查询评论
    List<Comment> findByBusinessIdAndRatingAndDeletedFalse(Long businessId, Integer rating);
    
    // 查询店铺的平均评分
    @Query("SELECT AVG(c.rating) FROM Comment c WHERE c.business.id = :businessId AND c.deleted = false")
    Double findAverageRatingByBusinessId(@Param("businessId") Long businessId);
    
    // 统计店铺的评论数量
    Long countByBusinessIdAndDeletedFalse(Long businessId);
    
    // 检查订单是否已评论
    boolean existsByOrderIdAndDeletedFalse(Long orderId);
    
    // 查询所有未删除的评论
    List<Comment> findByDeletedFalse();
}



